/*
 * @(#)UserpickerTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import java.sql.Connection;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.util.RequestUtils;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.WebUtil;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.dao.UserGroupDAObject;

/**
    UserpickerTag.java

    <p>This class is to extend the TextTag provided by Apache Struts.<p>
    <p>Modification in "struts-html.tld"<br>

  <pre>
     [ADDED]
     &lt;tag&gt;
     &lt;name&gt;userpicker&lt;/name&gt;
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.UserpickerTag&lt;/tagclass&gt;
     &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
     &lt;attribute&gt;
     &lt;name&gt;accesskey&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;alt&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;altKey&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;disabled&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;indexed&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;maxlength&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;name&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onblur&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onchange&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onclick&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;ondblclick&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onfocus&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onkeydown&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onkeypress&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onkeyup&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmousedown&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmousemove&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmouseout&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmouseover&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;onmouseup&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;property&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;readonly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;size&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;style&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;styleClass&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;styleId&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;tabindex&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;title&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;titleKey&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;value&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;attribute&gt;
     &lt;name&gt;textOnly&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
     &lt;/tag&gt;
     [ADDED]
  </pre>
  </p>

  <p>USAGE:</p>
  <p>If textOnly is set to true, only the full name of the user record, which id is the value of the hidden field, is shown. </p>
  <p>Otherwise there is an icon for poping up a box showing all user records, from which user can select a user record.</p>
  <p><code>&lt;html:userpicker property='reportToStaffRecordID' textOnly="true" /&gt;</code></p>

  @author          Wong Yam Lee
  @company         DCIVision Limited
  @creation date   12/08/2003
  @version         $Revision: 1.3.26.4 $
 */
public class GrouppickerTag extends TextTag {

  public static final String REVISION = "$Revision: 1.3.26.4 $";

  private String isMulitple = "";
  private String mulitpleValue = "";

  public String getIsMulitple() {
  return isMulitple;
  }

  public void setIsMulitple(String isMulitple) {
  this.isMulitple = isMulitple;
  }

  public String getMulitpleValue() {
  return mulitpleValue;
  }

  public void setMulitpleValue(String mulitpleValue) {
  this.mulitpleValue = mulitpleValue;
  }


  /** default constructor
   */
  public GrouppickerTag() {
    super();
  }

  public int doStartTag() throws JspException {
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    Object valueObj = RequestUtils.lookup(pageContext, name, property, null);
    String value = valueObj==null? "": valueObj.toString();
    String sPropertyName = this.getProperty();
    int retValue;

    // set the display value
    if ( Utility.isEmpty(value) ) {
      setValue("");
    } else {
      try {
        UserGroup userGroup = getUserGroupByID(new Integer(value));
        setValue(userGroup.getGroupName());
      } catch (ApplicationException appEx) {
        throw new JspException(appEx);
      }
    }
    setProperty("dummy_" + sPropertyName);
    setOnkeypress("return false;");   // disable keyboard input
    setOnkeydown("return false;");    // disable keyboard input
    retValue = super.doStartTag();
    setProperty(sPropertyName);

    try{
      JspWriter out = pageContext.getOut();

      // hidden field
      out.println("<input type='hidden' name='" + sPropertyName + "' value='" + value + "'>");

      // show the user picker if it is for editing
      if (!textOnly && !formTag.getTextOnly()) {
        String contextPath = (String)this.pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY);
        String sUrl = contextPath + "/user/PopupUserGroup.do?mode=L&property=" + sPropertyName + "&form=" + formTag.getBeanName() + "&isMulitple=" + this.getIsMulitple() + "&mulitpleValue=" + this.getMulitpleValue();
        String sName = "DCIVisionPopupGroup";
        String sPopW = "700";
        String sPopH = "500";
        String sFeature = "scrollbars=yes,resizable=yes";

        // <a> tag for displaying the user picker
        out.print(" <a href=\"javascript:void(0);\" onclick=\"return OpenCenteredPopup('" + sUrl + "', '" + sName + "', " + sPopW + ", " + sPopH + ", '" + sFeature + "');\">");

        //print the html img tag for search user image
        out.print("<img ");
        out.print("src=\""+WebUtil.getImage(pageContext.getRequest(), "IconSearchGroup.gif")+"\" ");
        out.print("align=\"absmiddle\" ");
        out.print("border=\"0\" ");
        out.print(">");

        // close the <a> tag
        out.println("</a>");
      }
    } catch (Exception e) {
      throw new JspException(e);
    }
    return retValue;
  }


  /** return a user record according to the given id
   *  @param    id
   *  @return   UserRecord
   *  @throws   ApplicationException
   */
  protected UserGroup getUserGroupByID(Integer id) throws ApplicationException {
    SessionContainer sessionCon = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection)this.pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    UserGroupDAObject dao = new UserGroupDAObject(sessionCon, conn);

    try {
      return (UserGroup)dao.getObjectByID(id);
    } catch (ApplicationException appEx) {
      throw appEx;
    }
  }

}
